package test;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.huonan.utils.ExecutorUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;

/**
 * @Description
 * @Author lxp
 * @Date 2022/5/13 下午4:27
 * @Version V1.0
 **/
@Slf4j
public class test {
    public static void main(String[] args) {
        String inPath = args[0];
        String outPath = args[1];
        String pathDep = args[2];

//        String arg1 = "/Users/lxp/IdeaProjects/Study/MainTest/data/test";
//        String arg2 = "/Users/lxp/IdeaProjects/Study/MainTest/data/new_test";

        ExecutorService executorService = ExecutorUtil.getExecutorService(10);
        List<File> files = FileUtil.loopFiles(Paths.get(inPath), Integer.parseInt(pathDep), null);
        for (File file : files) {
            CompletableFuture.runAsync(() -> {
                log.info("文件：{} Start", file);
                String fileAbsolutePath = file.getAbsolutePath();
                String fileName = file.getName();
                String tmpPath =   fileAbsolutePath.substring(0, fileAbsolutePath.lastIndexOf(fileName));
                String substring2 = tmpPath.substring(inPath.length());
                String newFile = outPath + File.separator +substring2+File.separator+ fileName;
                List<String> list = FileUtil.readUtf8Lines(file);
                List<String> newList = new ArrayList<>();
                for (String line : list) {
                    JSONObject obj = JSONUtil.parseObj(line);
                    obj.remove("kw_dataTagPlus");
                    JSONArray array = new JSONArray();
                    array.set("KOC");
                    JSONObject fobj = new JSONObject();
                    fobj.set("kw_dataTagPlus", array);
                    obj.set("data", fobj);
                    newList.add(JSONUtil.toJsonStr(obj));
                }
                FileUtil.appendUtf8Lines(newList, newFile);
                log.info("文件：{} end", file);
            }, executorService);
        }
        ExecutorUtil.closePool(executorService);
    }
    }